<%-- 
    Document   : controller
    Created on : 22/06/2012, 13:33:44
    Author     : Gledson
--%>
<%@page import="br.com.ZeroHora.DAO.usuarioDAO"%>
<%@page import="br.com.cadastros.bean.Usuario"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
           <% 
    try{
        String actionUsuario = request.getParameter("actionUsuario");
        
        if(actionUsuario.equalsIgnoreCase("cadastrar")){
            Usuario u = new Usuario();
            u.setNome(request.getParameter("nome"));
            u.setTelefone(request.getParameter("telefone"));
            u.setEmail(request.getParameter("email"));
            u.setSenha(request.getParameter("senha"));
            usuarioDAO uDao = new usuarioDAO();
                       
            try{
                uDao.cadastrar(u);
                request.setAttribute("msg", "Usuário Cadastrado com sucesso");
                request.getRequestDispatcher("cadastroUsuarios.jsp").forward(request, response);
            }catch(Exception erro){
                out.print("O erro foi: " + erro.getMessage());                                           
        }
              }
               else if(actionUsuario.equalsIgnoreCase("excluir")){
                   try{
                       Integer idUsuario = Integer.parseInt(request.getParameter("idUsuario"));
                       usuarioDAO uDao = new usuarioDAO();
                       Usuario u = uDao.busca(idUsuario);
                       if(u.getIdUsuario()>0){
                           uDao.delete(u);                       
                           request.setAttribute("msg", "Usuario Deletado com sucesso");
                           request.getRequestDispatcher("cadastroUsuario.jsp").forward(request, response);
            }          
                   }catch(Exception e){
                       out.print("Eroo: " + e.getMessage());
                   }                                                        
                                 }
               else if (actionUsuario.equalsIgnoreCase("update")) {
                    Usuario u = new Usuario();
                        u.setIdUsuario(Integer.parseInt(request.getParameter("idUsuario")));
                        u.setNome(request.getParameter("nome"));
                        u.setTelefone(request.getParameter("telefone"));
                        u.setEmail(request.getParameter("email"));
                        u.setEmail(request.getParameter("senha"));
                        usuarioDAO uDao = new usuarioDAO();
            
            try{
                uDao.update(u);
                request.setAttribute("msg", "Usuario Atualizado com sucesso");
                request.getRequestDispatcher("atualizarCliente.jsp").forward(request, response);
            }catch(Exception erro){
                out.print("Erro:" + erro.getMessage());
            }
                    
        }
        
               }catch(NullPointerException nexp){
                   out.print("Erro na operação");
               }
                                 
    %>

        
    </body>
</html>
